<template>
<div>
<template>

<!-- 添加宿管隐藏端口 -->
<el-dialog title="添加宿管" :visible.sync="dialogFormVisible">
  <el-form :model="hoursemaster">
    <el-form-item label="宿管姓名" label-width="120px">
      <el-input v-model="hoursemaster.username" autocomplete="off"></el-input>
    </el-form-item> 
    <el-form-item label="宿管密码" label-width="120px">
      <el-input v-model="hoursemaster.password" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="宿管工号" label-width="120px">
      <el-input  v-model="hoursemaster.housemasterNo" autocomplete="off"></el-input>
    </el-form-item>
      <el-form-item label="宿管邮箱" label-width="120px">
      <el-input v-model="hoursemaster.email" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label-width="200px">
      <el-button size="small"  type="primary" @click="ensureAddhoursemaster()">添加</el-button>
      <el-button size="small"  type="primary" @click="cancelAddhoursemaster()">取消</el-button>
    </el-form-item>
  </el-form>
  </el-dialog>

  <!-- 修改宿管隐藏端口 -->
    <el-dialog title="修改宿管" :visible.sync="updateDialogFormVisible">
  <el-form :model="uhoursemaster">
    <el-form-item label="宿管姓名" label-width="120px">
      <el-input v-model="uhoursemaster.username" autocomplete="off"></el-input>
    </el-form-item> 
    <el-form-item label="宿管密码" label-width="120px">
      <el-input v-model="uhoursemaster.password" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="宿管工号" label-width="120px">
      <el-input  v-model="uhoursemaster.housemasterNo" autocomplete="off"></el-input>
    </el-form-item>
      <el-form-item label="宿管邮箱" label-width="120px">
      <el-input v-model="uhoursemaster.email" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label-width="200px">
      <el-button size="small"  type="primary" @click="ensureUpdatehoursemaster()">修改</el-button>
      <el-button size="small"  type="primary" @click="cancelUpdatedhoursemaster()">取消</el-button>
    </el-form-item>
  </el-form>
  </el-dialog>

  <!-- 页面主体 -->
   <el-row :gutter="0">

     <!-- 添加宿管按钮 -->
    <el-col :span="3">
      <el-button size="small"  type="primary" @click="addhoursemaster()">增加宿管</el-button>
    </el-col>

    <!-- 输入框 -->
    <el-col :span="6">
        <el-input
        v-model="hoursemasterDTO.username"
        class="w-50 m-2"
        size="small"
        placeholder="请输入姓名"
        @input="search()"
        />
    </el-col>

    <!-- 搜索按钮 -->
    <el-col :span="3">
      <el-button size="small"  type="primary" @click="search()">搜索</el-button>
    </el-col>

     <!-- 导出Excel -->
    <el-col :span="3">
      <el-button size="small"  type="primary" @click="exportExcel()">导出Excel</el-button>
    </el-col>

  </el-row>

  <!-- 表格 -->
  <el-table :data="hoursemasters" stripe border height="300" id="mytable" >
    <el-table-column prop="id" sortable=true label="ID"/>
    <el-table-column prop="user.username" sortable=true label="昵称"/>
    <el-table-column prop="housemasterNo" sortable=true label="宿管号码" />
    <el-table-column prop="user.email" sortable=true label="角色号码" />
        <el-table-column label="操作">
            <template #default="scope">
                <el-button size="small" @click="updatehoursemaster(scope.row)">
                   修改
                </el-button>
                <el-button size="small" type="danger" @click="deletehoursemaster(scope.row.id)">
                   删除
                </el-button>
            </template>
            </el-table-column>
  </el-table>

  <!-- 分页部分 -->
  <el-pagination
    small
    background
    layout="prev, pager, next"
    :page-size="hoursemasterDTO.capacity"
    :current-page.sync="hoursemasterDTO.index"
    @current-change="changeCurrent"
    :total="count"
    class="mt-4"
  />
</template>

</div>
</template>
<script>
export default {
  name: 'Home',
  data () {
    return {
      // 当前登录的用户角色
      role: '宿管',
      // 表格映射对象
      hoursemasters: [],
      // 修改的对象
      uhoursemaster: {
        userJson: {
          '@type': 'com.nciae.po.User',
           username: '',
           password: '',
           email: '',
        },
           username: '',
           password: '',
           email: '',
           housemasterNo: ''  
      },
      // 表格查询对象
      hoursemasterDTO: {
          index: 1,
          capacity: 10,
          username: ''
      },
      // 角色列表
      roles: [],
      // 添加宿管弹出控制
      dialogFormVisible: false,
      // 修改宿管弹出控制
      updateDialogFormVisible: false,
      // 添加宿管实体
      hoursemaster: {
        userJson: {
          '@type': 'com.nciae.po.User',
        }
      },
      // 宿管列表总数
      count: 0,
      // 用户信息
      user: {},
      // 角色信息
      role: ''
    }
  },
  // 页面初始化函数 
  created () {

    // 获得角色信息
    this.role = window.sessionStorage.getItem('role')

    // 获得用户信息
    this.user = JSON.parse(window.sessionStorage.getItem('user'))

    this.$http({
        url: "/hoursemaster",
        params: {
            username: this.hoursemasterDTO.username
        }
    }).then(response => {
       this.hoursemasters = response.data
    })
  },
  methods: {
    // 改变当前页
    changeCurrent() {
      findAll()
    },
    // 添加宿管
     addhoursemaster() {
       this.dialogFormVisible=true;
     },
     updatehoursemaster(data) {
       this.uhoursemaster.id = data.user.id
       this.uhoursemaster.username=data.user.username
       this.uhoursemaster.password=data.user.password
       this.uhoursemaster.email=data.user.email
       this.uhoursemaster.housemasterNo=data.housemasterNo
       this.updateDialogFormVisible=true
     },
     deletehoursemaster(id) {
        this.$http.delete('/hoursemaster/' + id).then(response => {
          this.$message({
          message: '删除宿管成功',
          type: 'success'
          });
          this.findAll()
        })
     },
     ensureAddhoursemaster() {
      this.hoursemaster.userJson.username=this.hoursemaster.username
      this.hoursemaster.userJson.password=this.hoursemaster.password
      this.hoursemaster.userJson.email=this.hoursemaster.email
      this.hoursemaster.userJson = JSON.stringify(this.hoursemaster.userJson)
      this.$http.post("/hoursemaster", this.hoursemaster).then(response => {
        this.$message({
          message: '添加宿管成功',
          type: 'success'
        });
        this.clearhoursemaster()
        this.findAll()
      })
    },
    // 取消添加宿管
    cancelAddhoursemaster() {
      this.clearhoursemaster()
    },
    ensureUpdatehoursemaster() {
      this.uhoursemaster.userJson.username=this.uhoursemaster.username
      this.uhoursemaster.userJson.password=this.uhoursemaster.username
      this.uhoursemaster.userJson.email=this.uhoursemaster.email
      this.uhoursemaster.userJson = JSON.stringify(this.uhoursemaster.userJson)
      this.$http.put('/hoursemaster', this.uhoursemaster).then(response => {
        this.$message({
          message: '修改宿管成功',
          type: 'success'
        });   
        this.updateDialogFormVisible=false
        this.findAll()
      })
    },
    cancelUpdatedhoursemaster() {
      this.updateDialogFormVisible=false
    },
    // 搜索对象，并映射
    search() {
      this.findAll()
    },
    // 查询所有，并映射
    findAll() {
    this.$http({
        url: "/hoursemaster",
        params: {
            username: this.hoursemasterDTO.username
        }
    }).then(response => {
       this.hoursemasters = response.data
    })
    },
    // 清除宿管，并关闭窗口
    clearhoursemaster() {
        this.hoursemaster.username=''
        this.hoursemaster.password=''
        this.hoursemaster.email=''
        this.hoursemaster.housemasterNo=''
        this.hoursemaster.userJson = {'@type': 'com.nciae.po.User'}
        this.dialogFormVisible=false
     },

    // 导出Excel
    exportExcel () {
        var xlsxParam = { raw: true } 
        var wb = this.$xlsx.utils.table_to_book(document.querySelector('#mytable'), xlsxParam)
        /* get binary string as output */
        var wbout = this.$xlsx.write(wb, { bookType: 'xlsx', bookSST: true, type: 'array' })
        try {
            this.$filesaver.saveAs(new Blob([wbout], { type: 'application/octet-stream' }), this.user.username + '导出宿管信息表.xlsx')
        } catch (e) {
        if (typeof console !== 'undefined') {
            console.log(e, wbout)
        }
        }
        return wbout
    }
  }
}
</script>
<style>

</style>